# -*- coding: utf-8 -*-
"""
导出器基类
"""
from abc import ABC, abstractmethod
from typing import Dict, List, Any
from datetime import datetime
import os

class BaseExporter(ABC):
    """导出器基类"""
    
    def __init__(self):
        self.progress_callback = None
        self.cancel_flag = False
    
    def set_progress_callback(self, callback):
        """设置进度回调函数"""
        self.progress_callback = callback
    
    def set_cancel_flag(self, cancel_flag):
        """设置取消标志"""
        self.cancel_flag = cancel_flag
    
    def update_progress(self, progress: int, message: str = ""):
        """更新进度"""
        if self.progress_callback:
            self.progress_callback(progress, message)
    
    def check_cancel(self) -> bool:
        """检查是否取消"""
        return self.cancel_flag
    
    @abstractmethod
    def export(self, dict_data: Dict, file_path: str) -> bool:
        """
        导出数据库字典
        
        Args:
            dict_data: 数据库字典数据
            file_path: 导出文件路径
            
        Returns:
            导出是否成功
        """
        pass
    
    def _validate_file_path(self, file_path: str) -> bool:
        """验证文件路径"""
        try:
            # 检查目录是否存在
            directory = os.path.dirname(file_path)
            if not os.path.exists(directory):
                os.makedirs(directory)
            
            # 检查是否有写入权限
            if not os.access(directory, os.W_OK):
                return False
            
            return True
        except Exception:
            return False
    
    def _get_export_summary(self, dict_data: Dict) -> Dict:
        """获取导出摘要信息"""
        return {
            'export_time': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            'database_name': dict_data.get('database_info', {}).get('database_name', ''),
            'database_version': dict_data.get('database_info', {}).get('version', ''),
            'total_tables': dict_data.get('total_tables', 0),
            'processed_tables': dict_data.get('processed_tables', 0),
            'total_columns': sum(len(table.get('columns', [])) for table in dict_data.get('tables', [])),
            'total_indexes': sum(len(table.get('indexes', [])) for table in dict_data.get('tables', []))
        }
